package org.lds.scheduled.job.admin.starter.health;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.lds.scheduled.job.admin.business.worker.model.request.WorkerBaseListPageRequest;
import org.lds.scheduled.job.admin.business.worker.model.request.WorkerBaseUpdateRequest;
import org.lds.scheduled.job.admin.business.worker.model.response.WorkerBaseResponse;
import org.lds.scheduled.job.admin.business.worker.service.WorkerBaseService;
import org.lds.scheduled.job.common.model.dto.ResultDTO;
import org.lds.scheduled.job.common.model.db.PageResult;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RequiredArgsConstructor
public class WorkerHealthCheckStarter extends AbstractInstanceHealthCheckStarter<WorkerBaseResponse> {
    private final WorkerBaseService workerBaseService;

    @Override
    protected ResultDTO<PageResult<WorkerBaseResponse>> pageInstances() {
        WorkerBaseListPageRequest request = new WorkerBaseListPageRequest();
        request.setGtId(getId());
        request.setPageNum(1);
        request.setPageSize(500);
        request.setOrderByKey("id");
        request.setOrderByDesc(false);
        return workerBaseService.listWorkerPage(request);
    }

    @Override
    protected Long id(WorkerBaseResponse instance) {
        return instance.getId();
    }

    @Override
    protected String address(WorkerBaseResponse instance) {
        return instance.buildAddress();
    }

    @Override
    protected boolean enable(WorkerBaseResponse instance) {
        return instance.getEnable();
    }

    @Override
    protected void updateInstance(Long id, boolean enable) {
        workerBaseService.updateWorker(WorkerBaseUpdateRequest.builder().id(id).enable(enable).build());
        log.info("worker状态更新 id={} enable={}", id, enable);
    }

}
